import userCheckInModel from "../model/userCheckInModel";
import dTalkApi from "../core/dTalkApi";
import visitMonthPlanModel from "../model/visitMonthPlanModel";
import * as moment from 'moment';
import dTalkApiUtil from 'dingtalk-app/dtalk/dTalkApiUtil';

class UserCheckinService{
    constructor(){}

    //获取签到的数据,保存员工的签到数据
    async saveUserCheckinData(date:string){
        console.log('当天保存签到数据开始!!!!!!!!!!!!!1');
        let userIdList:string[]=[];
        let month=moment(date).format('MM');
        let year=moment(date).format('YYYY');
        userIdList=await visitMonthPlanModel.distinct("userId",{});
        // let date3=moment().format('2018-10-15 09:00:00');
        // let time2=moment().format('2018-10-15 HH:mm:ss');
        // let date1="2018-10-15";
        let date3=moment().format('YYYY-MM-DD 09:00:00');
        let time2=moment().format('YYYY-MM-DD HH:mm:ss');
        let date1=moment().format('YYYY-MM-DD');
        let time=new Date(date3).getTime();
        //let dateList=await visitMonthPlanModel.distinct("planDate",{month:month,year:year});
        let dateList=await visitMonthPlanModel.distinct("planDate",{month:month});
        //获取数据
        let list:any=[]
        let access_token=await dTalkApiUtil.getAccessToken();
        if(dateList.includes(date1)){
            list =await dTalkApi.getCheckInData_oneDay(userIdList,access_token);
            let dataCach:any[]=[]
            for (let index = 0; index < list.length; index++) {
                let element = list[index];
                let userid=element.userId;
                let checkin_time:any=''
                let isCheckInNineAm:boolean=false;
                let d2:any='';
                if(dataCach.includes(userid)){
                    continue
                }else{
                    dataCach.push(userid) 
                }
                // console.log(element,'11111111111111111111',index,'<------------------',element.data.checkin_time)
                if(element.data !=''){
                    checkin_time=JSON.parse(element.data.checkin_time);
                    if(time-checkin_time>=0){
                        isCheckInNineAm=true;
                    }else{
                        isCheckInNineAm=false;
                    }
                    let date3 = new Date(checkin_time);
                    d2 = moment(date3).format('YYYY-MM-DD HH:mm:ss');
                    let d3 = moment(date3).format('HH:mm:ss');
                }
                let a=await userCheckInModel.create({
                    sourceId:userid+'&'+date1,
                    date_:date1,
                    year:year,
                    month:month,
                    userId:userid,
                    checkin_time:d2,
                    isCheckInNineAm:isCheckInNineAm,//是否是上午9:00前签到
                    time_getCheckData:time2
                });
                // console.log(a,'aaaaaaaaaaaaaa',index);
            }
            console.log('当天保存签到数据完成!!!!!!!!!!!!!1');
        }
        return list
    }


}
let checkIn=new UserCheckinService();

export default checkIn;